package test230605;

import java.util.Arrays;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * @date 2023/6/5 11:50
 * 467. 环绕字符串中唯一的子字符串
 * https://leetcode.cn/problems/unique-substrings-in-wraparound-string/
 */
@SuppressWarnings({"all"})
public class Solution02 {

    public int findSubstringInWraproundString(String ss) {
        int n = ss.length();
        char[] s = ss.toCharArray();
        int[] dp = new int[n];  // dp[i] 表示以 i 位置字符结尾的子串的个数
        Arrays.fill(dp, 1);  // 初始状态均为 1
        for (int i = 1; i < n; ++i) {
            if (s[i] - 1 == s[i - 1] || s[i] == 'a' && s[i - 1] == 'z') {
                dp[i] = dp[i - 1] + 1;
            }
        }
        // 去重
        int[] array = new int[26];
        for (int i = 0; i < n; ++i) {
            array[s[i] - 'a'] = Math.max(array[s[i] - 'a'], dp[i]);
        }
        // 求和
        int retSum = 0;
        for (int item : array) {
            retSum += item;
        }
        return retSum;
    }
}
